/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.taxigang.shared;

import java.io.Serializable;
import java.util.Date;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.TextArea;





public class TaxiGangInfo implements Serializable,Comparable<TaxiGangInfo> {
	private static final String CLOSED = "CLOSED";
	public enum LeaveResult {
		OK, CANT_LEAVE, LEFT_BUT_STILL_MEMBER
	}

	public TaxiGangInfo(){
		//TODO GWT has TimeZone which may be better than the deprecated Date method
		timeZoneOffset = new Date().getTimezoneOffset();
	}
	private long id;
    private String start;
    private String destination;
    private String postalCode;
    private double startLat;
    private double startLng;
    private double destinationLat;
    private double destinationLng;
    private String status;
    //private String leaderName;
    private Long[]memberIds;
    private TaxiGanguserInfo[]members;
//    private String member1Name;
//    private String member2Name;
//    private String member3Name;
    
	private String link;
    private String locale;
    private String page;
    private String pageAccessToken;

    private int timeZoneOffset;
    private int departureHour;
    private int departureMinute;
    private Date departureLocalTime;
	int departureDay;
	int departureMonth;
	int departureYear;
    private String departureTime;
    private Date creationTime;
    private ChitInfo[] chitList;
    private String invitationMessage;
//    private int numMembers;
    private String[] messages;
    
    private String meetingMessage;
    public String getMeetingMessage() {
		return meetingMessage;
	}

	public void setMeetingMessage(String meetingMessage) {
		this.meetingMessage = meetingMessage;
	}
	private double affinity;
     
     public long getId() {
         return id;
     }

    public void setId(long id) {
        this.id = id ;
    }


    public double getStartLat() {
        return startLat;
    }
    public void setStartLat(double lat) {
        startLat = lat;
    }

    public double getStartLng() {
        return startLng;
    }
    public void setStartLng(double lng) {
        startLng = lng;
    }

    public double getDestinationLat() {
        return destinationLat;
    }
    public void setDestinationLat(double lat) {
        destinationLat = lat;
    }

    public double getDestinationLng() {
        return destinationLng;
    }
    public void setDestinationLng(double lng) {
        destinationLng = lng;
    }

    public Date getLocalDepartureTime() {
        return departureLocalTime;
    }
    public void setLocalDepartureTime(Date time) {
        departureLocalTime = time;
    }
    public int getDepartureHour() {
        return departureHour;
    }
    public int getDepartureMinute() {
        return departureMinute;
    }
    public void setDepartureHour(int hour) {
        this.departureHour = hour;
    }
    public void setDepartureMinute(int minute) {
        this.departureMinute = minute;
    }
    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date time) {
        creationTime = time;
    }

    public String getLeaderName(){
       	if (members.length > 0){
    		return members[0].getDisplayName();
    	}
    	return "error";
    }
    public String getMemberName(int i){
    	i++;
    	if (memberIds.length > i){
    		return members[i].getDisplayName();
    	}
    	return "error";
//    	String name="";
//    	switch (i){
//    	case 0: name = member1Name; break;
//    	case 1: name = member2Name; break;
//    	case 2: name = member3Name; break;
//    	default:
//    		Log.error("out of bounds"+i);
//    		throw new RuntimeException("out of bounds:"+i);
//    	}
//    	return name;
    }

    public void setMemberIds(Long[] members){
    	this.memberIds = members;
//    	switch (i){
//    	case 1: member1Name = name; break;
//    	case 2: member2Name = name; break;
//    	case 3: member3Name = name; break;
//    	default:
//    		throw new RuntimeException("out of bounds");
//    	}
    }

    public int getNumMembers() {
        return memberIds.length;
    }
    
    public Long[] getMemberIds() {
        return memberIds;
    }

    public TaxiGanguserInfo[] getMember() {
		return members;
	}

	public void setMembers(TaxiGanguserInfo[] members) {
		this.members = members;
	}

//    public void setNumMembers(int numMembers) {
//        this.numMembers = numMembers;
//    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    
    public String getStart() {
        return start;
    }
    public String getDestination() {
        return destination;
    }


    public void setStart(String start) {
        this.start = start;
    }
    public void setDestination(String destination) {
        this.destination=destination;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }
    public String getPostalCode() {
        return postalCode;
    }

    public ChitInfo[] getChitList(){
    	if (chitList==null){
    		chitList = new ChitInfo[0];
    	}
    	return chitList;
    }
    public void setChitInfo(ChitInfo[] infoList){
    	chitList=infoList;
    }
		
    public String[] getMessages(){
    	return messages;
    }
    public void setMessages(String[] messages){
    	this.messages=messages;
    }
		
	public double getAffinity(){
		return affinity;
	}

	public void setAffinity(double affinity){
		this.affinity = affinity;
	}

	public int getTimeZoneOffset(){
		return timeZoneOffset;
	}

	public int compareTo(TaxiGangInfo info){
		if (status == null || info.getStatus() == null){
			//TODO log this
			return 0;
		}
		if (status.equals("OPEN") && info.getStatus().equals("CLOSED")){
			return -1;
		}
		if (status.equals("CLOSED") && info.getStatus().equals("OPEN")){
			return 1;
		}
		int minutesToGo = (departureHour - info.getDepartureHour())*4 + (departureMinute - info.getDepartureMinute());
		int result = 0;
		double affintyDifference = (affinity - info.getAffinity())* 24*60 + minutesToGo;
		if (affintyDifference > 0){
			result = 1 ;
		} 
		if (affintyDifference < 0){
			result = -1 ;
		} 
		return result;
		
	}
	
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof TaxiGangInfo)) {
            return false;
        }
        return this.id == ((TaxiGangInfo)object).id;
    }


    public String getDepartureTime() {
		return departureTime;
	}
	public void setDepartureTime(String departureTime) {
		this.departureTime=departureTime;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link){
		this.link = link;
	}

	public String getLocale() {
		return locale;
	}
	public void setLocale(String locale) {
		this.locale=locale;
	}

	public void setPage(String page) {
		this.page = page;		
	}
	public String getPage() {
		return page;		
	}
	public void setPageAccessToken(String pageAccessToken) {
		this.pageAccessToken = pageAccessToken;		
	}
	public String getPageAccessToken() {
		return pageAccessToken;		
	}

	public boolean isClosed() {
		return CLOSED.equals(status);
		
	}

	public String getInvitationMessage() {
		return invitationMessage;
	}
	
	public void setInvitationMessage(String invitationMessage){
		this.invitationMessage = invitationMessage;
	}

	public void setDepartureDate(int day, int month, int year) {
		departureDay = day;
		departureMonth = month;
		departureYear = year;
	}
    
	public int getDepartureDay() {
		return departureDay;
	}
	public int getDepartureMonth() {
		return departureMonth;
	}
	public int getDepartureYear() {
		return departureYear;
	}
    
}
